package com.wuheng.smartlock;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity implements OnCheckedChangeListener {
	ComponentName componentName;
	DevicePolicyManager policyManager;
	CompoundButton mButton, mButtonProximity, mButtonWake, mButtonUnlock;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mButton = (CompoundButton) findViewById(R.id.toggleButton1);
		mButtonProximity = (CompoundButton) findViewById(R.id.switch1);
		mButtonWake = (CompoundButton) findViewById(R.id.wake);
		mButtonUnlock = (CompoundButton) findViewById(R.id.unlock);
		policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

		if (!policyManager.isAdminActive(componentName)) {// ???????(????????)
			activeManager();
		}
		// if (!MyService.getRunningState() && Prefs.isEnable(this)) {
		Intent intent = new Intent(this, MyService.class);
		startService(intent);
		// }
		mButton.setChecked(Prefs.isEnable(this));
		mButtonProximity.setChecked(Prefs.isProximityEnable(this));
		mButtonWake.setChecked(Prefs.isWakeEnable(this));
		mButtonUnlock.setChecked(Prefs.isUnlockEnable(this));
		mButtonProximity.setEnabled(mButton.isChecked());
		mButtonUnlock.setEnabled(mButtonWake.isChecked());
		mButton.setOnCheckedChangeListener(this);
		mButtonProximity.setOnCheckedChangeListener(this);
		mButtonWake.setOnCheckedChangeListener(this);
		mButtonUnlock.setOnCheckedChangeListener(this);
	}

	private void activeManager() {
		// 使用隐式意图调用系统方法来激活指定的设备管理器
		componentName = new ComponentName(this, LockReceiver.class);
		Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
		intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
		intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "一键锁屏");
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onResume() {
		// TODO 自动生成的方法存根
		super.onResume();
		if (!policyManager.isAdminActive(componentName)) {// ???????(????????)
			activeManager();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO 自动生成的方法存根
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 0) {
			if (resultCode != Activity.RESULT_OK) {
				finish();
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO 自动生成的方法存根
		switch (item.getItemId()) {
		case R.id.action_settings:
			Intent i = new Intent(this, About.class);
			startActivity(i);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO 自动生成的方法存根
		switch (buttonView.getId()) {
		case R.id.toggleButton1: {
			Prefs.setEnable(this, isChecked);
			mButtonProximity.setEnabled(isChecked);
			break;
		}

		case R.id.switch1:
			Prefs.setProximityEnable(this, isChecked);
			break;
		case R.id.wake:
			Prefs.setWakeEnable(this, isChecked);
			mButtonUnlock.setEnabled(isChecked);
			break;
		case R.id.unlock:
			Prefs.setUnlockEnable(this, isChecked);
			break;
		}
	}

}
